package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class SystemException {
    //JSONP报错，返回值 callback(json) 如果请求的参数中包含callback，则标识为跨域请求

    @ExceptionHandler({RuntimeException.class})
    public Object fail(Exception e, HttpServletRequest request){

        String callback=request.getParameter("callback");
        if (StringUtils.isEmpty(callback)){
            //callback为空则表示不为跨域请求
            e.printStackTrace();
            return SysResult.fail();
        }else {
            //callback不为空，表示是跨域请求
            SysResult sysResult=SysResult.fail();
            return new JSONPObject(callback, sysResult);
        }
    }
}
